﻿<%@ WebHandler Language="C#" Class="School" %>

using System;
using System.Web;
using System.Data;
using System.Collections.Generic;
using Company.StudyClub.Model;
using Company.StudyClub.BLL.School;
using Company.StudyClub.BLL.Common;
using System.Text;

public class School : IHttpHandler {

    private HttpHelper helper = new HttpHelper();
    
    public void ProcessRequest (HttpContext context) {
        HttpResponse response = context.Response;
        switch (helper.GetString("type"))
        {
            case "addSchool":
                AddSchool(response);
                break;
            case "schoolsum":
                GetTotalSearchSchools(response);
                break;
            case "searchschool":
                SearchSchools(response);
                break;
            case "deleteSchool":
                DeleteSchool(response);
                break;
            case "editschool":
                EditSchool(response);
                break;
        }
    }

    private void EditSchool(HttpResponse response)
    {
        int schoolId = helper.GetInt("schoolid");
        string schoolName = helper.GetString("schoolname");
        string address = helper.GetString("address");
        string description = helper.GetString("description");
        int category = helper.GetInt("cate");
        
        SchoolBLL objBLL = new SchoolBLL();
        int result = objBLL.UpdateSchool(schoolId, schoolName, address, description, category);
        if (result > 0)
        {
            response.Write("1");
        }
        else
        {
            response.Write("0");
        }
    }
    
    /// <summary>
    /// 添加学校
    /// </summary>
    private void AddSchool(HttpResponse response)
    {
        School_Info model = new School_Info();
        model.SchoolName = helper.GetString("schoolName");
        model.RegionID = helper.GetInt("regionID");
        model.Category = helper.GetInt("category");
        model.Address = helper.GetString("address");
        model.AddTime = DateTime.Now;
        model.Description = helper.GetString("description");

        SchoolBLL objBLL = new SchoolBLL();
        if (objBLL.Add(model) > 0)
        {
            response.Write("1");
        }
        else
        {
            response.Write("0");
        }
    }

    #region 根据条件得到学校
    private void GetTotalSearchSchools(HttpResponse response)
    {
        string schoolname = helper.GetString("schoolname");
        int regionId = helper.GetInt("regionId");
        int category = helper.GetInt("category");

        SchoolBLL objBLL = new SchoolBLL();

        response.Write(objBLL.GetTotalSchoolsBySearch(regionId,schoolname,category));
    }

    private void SearchSchools(HttpResponse response)
    {
        int pageIndex = helper.GetInt("pageindex");
        int pageSize = helper.GetInt("pagesize");
        string schoolname = helper.GetString("schoolname");
        int regionId = helper.GetInt("regionId");
        int category = helper.GetInt("category");

        SchoolBLL objBLL = new SchoolBLL();

        List<School_Info> objList = objBLL.SearchSchools(pageIndex, pageSize, regionId, schoolname, category);

        StringBuilder objSB = new StringBuilder();

        objSB.Append("[");
        for (int i = 0; i < objList.Count; i++)
        {
            School_Info model = (School_Info)objList[i];
            objSB.Append("{\"schoolId\":\"");
            objSB.Append(model.SchoolID);
            objSB.Append("\",\"schoolName\":\"");
            objSB.Append(model.SchoolName);
            objSB.Append("\",\"regionId\":\"");
            objSB.Append(model.RegionID);
            objSB.Append("\",\"category\":\"");
            objSB.Append(model.Category);
            objSB.Append("\",\"address\":\"");
            objSB.Append(model.Address);
            objSB.Append("\",\"description\":\"");
            objSB.Append(model.Description);
            objSB.Append("\",\"row\":\"");
            objSB.Append(model.RowNumber);
            objSB.Append("\"}");

            if (i != objList.Count - 1)
                objSB.Append(",");
        }
        objSB.Append("]");

        response.Write(objSB.ToString());
    }
    #endregion

    #region 获得该地区下所有的学校
    private void GetTotalSchools(HttpResponse response)
    {
        SchoolBLL objBLL = new SchoolBLL();

        response.Write(objBLL.GetTotalSchools(helper.GetInt("regionID")).ToString());
    }

    private void GetSchoolsByRegionID(HttpResponse response)
    {
        SchoolBLL objBLL = new SchoolBLL();

        List<School_Info> objList = objBLL.GetSchoolsByRegionID(helper.GetInt("pageIndex"), helper.GetInt("pageSize"), helper.GetInt("regionID"));

        StringBuilder objSB = new StringBuilder();

        objSB.Append("[");
        for (int i = 0; i < objList.Count; i++)
        {
            School_Info model = (School_Info)objList[i];
            objSB.Append("{\"schoolId\":\"");
            objSB.Append(model.SchoolID);
            objSB.Append("\",\"schoolName\":\"");
            objSB.Append(model.SchoolName);
            objSB.Append("\",\"regionId\":\"");
            objSB.Append(model.RegionID);
            objSB.Append("\",\"category\":\"");
            objSB.Append(model.Category);
            objSB.Append("\",\"address\":\"");
            objSB.Append(model.Address);
            objSB.Append("\",\"description\":\"");
            objSB.Append(model.Description);
            objSB.Append("\",\"row\":\"");
            objSB.Append(model.RowNumber);
            objSB.Append("\"}");

            if (i != objList.Count - 1)
                objSB.Append(",");
        }
        objSB.Append("]");

        response.Write(objSB.ToString());
    }
    #endregion

    private void DeleteSchool(HttpResponse response)
    {
        try
        {
            string[] sids = helper.GetString("sids").Split(',');
            for (int i = 0; i < sids.Length; i++)
            {
                if (sids[i] == string.Empty)
                    continue;

                SchoolBLL objBLL = new SchoolBLL();
                objBLL.Delete(int.Parse(sids[i]));
            }
            response.Write("1");
        }
        catch
        {
            response.Write("0");
        }
    }
    
    public bool IsReusable {
        get {
            return false;
        }
    }

}